k, can the graph be coloured with at most k colours?
Procedure KnapsackBruteForceBitmask(W, V, C):
n ← length(W)
bestValue ← 0
For mask ← 0 to 2^n - 1 do
totalW ← Σ (W[i] * bit(mask, i)) // bit(mask,i) = 0 or 1
totalV ← Σ (V[i] * bit(mask, i))
if totalW ≤ C then
bestValue ← max(bestValue, totalV)
end if
end for
return bestValueProcedure KnapsackRecursive(W, V, C, i):
// W = weights, V = values, C = remaining capacity, i = current index
if i = length(W) or C = 0 then
return 0
if W[i] > C then
// Cannot include this item
return KnapsackRecursive(W, V, C, i+1)
else
// Option 1: skip this item
skip ← KnapsackRecursive(W, V, C, i+1)
// Option 2: include this item
take ← V[i] + KnapsackRecursive(W, V, C - W[i], i+1)
return max(skip, take)## Algorithm
G # graph
Nodes # array/list of vertices in G, e.g. [v1, v2, ..., vn]
color # dictionary: vertex -> {0,1,2,3} (0 means uncoloured)
COLORS = [1, 2, 3]
function ColorDFS(pos):
# Base case: all vertices assigned a color
if pos = length(Nodes):
return True
v ← Nodes[pos]
# Try each color for v
for c in COLORS:
if IsValidColor(v, c):
color[v] ← c # MAKE
if ColorDFS(pos + 1): # Recurse to next vertex
return True
color[v] ← 0 # UNMAKE (backtrack)
return False # No color worked for v
function IsValidColor(v, c):
for w in Neighbours(v):
if color[w] = c: # Adjacent same color? invalid
return False
return TrueO(V+E).C.V*, is there a subset with total value ≥ V* and total weight ≤ C? (NP-complete)2^n subsets; each bit decides include/exclude.Procedure KnapsackBitmask(W, V, C):
n ← length(W)
bestValue ← 0
For mask ← 0 to 2^n − 1 do
totalW ← Σ (W[i] * bit(mask, i)) // bit(mask,i) ∈ {0,1}
totalV ← Σ (V[i] * bit(mask, i)) // multiply by mask bit
if totalW ≤ C then
bestValue ← max(bestValue, totalV)
end if
end for
return bestValueProcedure KnapsackRecursive(W, V, C, i):
if i = length(W) or C = 0 then
return 0
if W[i] > C then
return KnapsackRecursive(W, V, C, i+1)
skip ← KnapsackRecursive(W, V, C, i+1)
take ← V[i] + KnapsackRecursive(W, V, C - W[i], i+1)
return max(skip, take)Procedure KnapsackTopDown(W, V, C):
memo ← dictionary // keys: (i, c)
Function F(i, c):
if i = length(W) or c = 0 then return 0
if (i, c) in memo then return memo[(i, c)]
if W[i] > c then
ans ← F(i+1, c)
else
ans ← max( F(i+1, c), V[i] + F(i+1, c - W[i]) )
end if
memo[(i, c)] ← ans
return ans
return F(0, C)Procedure KnapsackBottomUp(W, V, C):
n ← length(W)
dp ← array (n+1) × (C+1) filled with 0
for i ← 1 to n do
for c ← 0 to C do
dp[i][c] ← dp[i-1][c]
if W[i-1] ≤ c then
dp[i][c] ← max(dp[i][c], V[i-1] + dp[i-1][c - W[i-1]])
end if
end for
end for
return dp[n][C]Procedure KnapsackBacktrack(W, V, C):
n ← length(W)
best ← 0
// optional: order items by value/weight to tighten bounds
Procedure DFS(i, w, v, upperBound):
if w > C then return
if v + upperBound ≤ best then return // bound prune
if i = n then
best ← max(best, v)
return
end if
// compute new upper bound (e.g., fractional greedy from item i)
ubNext ← BoundFrom(i, w, v)
// try include
DFS(i+1, w + W[i], v + V[i], ubNext)
// try exclude
DFS(i+1, w, v, ubNext)
DFS(0, 0, 0, BoundFrom(0, 0, 0))
return bestHeuristic / metaheuristic methods
Perfect — here’s the Travelling Salesman Problem (TSP) section in the same style you used for Graph Colouring and Knapsack, with brute force (bitmask/permutation), backtracking, and heuristics.
K, is there a tour of total cost ≤ K?
n−1 cities.
(n−1)! tours, but since each can be traversed in reverse, only (n−1)! / 2 are unique.Procedure TSP_BruteForce(D):
n ← number of cities
bestCost ← ∞
For each permutation P of {2..n} do
tour ← [1] + P + [1]
cost ← Σ D[tour[i]][tour[i+1]]
bestCost ← min(bestCost, cost)
end for
return bestCostProcedure Permute(list, k, n):
if k = n then
output list
else
For i from k to n do
swap(list[k], list[i])
Permute(list, k+1, n)
swap(list[k], list[i]) // backtrack
EndForBacktracking (branch & bound)
Greedy nearest neighbour
Minimum spanning tree (MST) approximation
Hill climbing (local search)
Simulated annealing
A* search with lower bounds
f = g + h.g = cost so far, h = lower bound on remaining cost (e.g. MST of unvisited cities).